package com.muyuan.business.strategy.impl;

import com.muyuan.business.domain.TbBusiness;
import com.muyuan.business.strategy.Rule;
import com.muyuan.clues.domain.TbAssignRecord;
import com.muyuan.clues.mapper.SysDictDataMapper;
import com.muyuan.clues.mapper.SysUserMapper;
import com.muyuan.clues.mapper.TbAssignRecordMapper;
import com.muyuan.common.core.domain.entity.SysDictData;
import com.muyuan.common.core.domain.entity.SysUser;
import com.muyuan.common.utils.DateUtils;
import com.muyuan.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 *  rule 处理策略
 *  1.默认会把转化的线索分配给自己进行跟进。
 */
@ConditionalOnProperty(name = "rule.transfor", havingValue = "rule")
@Service("BussinessruleStrategy")
public class RuleStrategy implements Rule {

    //这里能够成功注入吗？
    @Autowired
    private static TbAssignRecordMapper assignRecordMapper;

    //这里能够成功注入吗？
    @Autowired
    private static SysUserMapper userMapper;

    @Autowired
    private static SysDictDataMapper dictDataMapper;

    private static SysUser thisUser = new SysUser();
    /**
     * 参考一下adminStrategy是怎么做的
     */
    static{
        //空间换时间的方式将数据库中的学科读取到内存中
        //预加载学科数据到内存中
        // 获取当前登录的用信息
        String username = SecurityUtils.getUsername();
        SysUser thisUser = userMapper.selectUserByName(username);
    }


    /**
     * 定义一些规则来自动
     * @param tbBusiness
     * @return
     */
    @Override
    public Integer transforBusiness(TbBusiness tbBusiness) {
        return this.distribute(tbBusiness,thisUser);
    }


    /**
     * 分配商机给具体用户的方法
     * 看我看我！！！！！！！！！！！！！！！
     * 看我能少走弯路！！！！！！！！！！！！
     * 这部分代码没有预设bug，不用研究
     * 该方法主要是将商机分配给具体的人的方法
     * @param business
     * @param user
     * @return
     */
    private int distribute(TbBusiness business,SysUser user){
        TbAssignRecord tbAssignRecord =new TbAssignRecord();
        tbAssignRecord.setAssignId(business.getId());
        tbAssignRecord.setUserId(user.getUserId());
        tbAssignRecord.setUserName(user.getUserName());
        tbAssignRecord.setDeptId(user.getDeptId());
        tbAssignRecord.setCreateBy(SecurityUtils.getUsername());
        tbAssignRecord.setCreateTime(DateUtils.getNowDate());
        tbAssignRecord.setType(TbAssignRecord.RecordType.BUSNIESS.getValue());
        business.setNextTime(null);
        return assignRecordMapper.insertAssignRecord(tbAssignRecord);
    }

}
